//
//  QuickLookFileController.m
//  TrainTree
//
//  Created by wanghui on 2017/9/1.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "QuickLookFileController.h"
#import <WebKit/WebKit.h>
@interface QuickLookFileController ()<UIWebViewDelegate>
{
    UIActivityIndicatorView *_load;
}
@property(nonatomic,strong)UIWebView *webView;

@end

@implementation QuickLookFileController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationView.hidden = NO;
    [self.view addSubview:self.webView];

    if(self.filePath){
        NSURL *url = [NSURL URLWithString:self.filePath];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];
    }

    [self.navigationView.leftBtn addTarget:self action:@selector(backLastVC) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationView.leftBtn setImage:ImageNamed(@"backBlack") forState:UIControlStateNormal];
    self.navigationView.line.hidden = NO;
    [self createLoadView];
}
- (void)createLoadView
{
    _load = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(ScreenWidth / 2 - 40, ScreenHeight / 2 - 40, 80, 80)];
    _load.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    _load.alpha = 0.6;
    [self.view addSubview:_load];
}
#pragma mark - WebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [_load startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // 缩放//(initial-scale是初始缩放比,minimum-scale=1.0最小缩放比,maximum-scale=5.0最大缩放比,user-scalable=yes是否支持缩放)
    NSString *jsMeta = [NSString stringWithFormat:@"var meta = document.createElement('meta');meta.content='width=device-width,initial-scale=1.0,minimum-scale=.5,maximum-scale=3';meta.name='viewport';document.getElementsByTagName('head')[0].appendChild(meta);"];
    [_webView stringByEvaluatingJavaScriptFromString:jsMeta];

    [_load stopAnimating];
    [_load removeFromSuperview];

}

- (void)backLastVC
{
    [self.navigationController popViewControllerAnimated:YES];
    self.webView = nil;
    [self.webView removeFromSuperview];
}
- (UIWebView *)webView
{
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64)];
        _webView.delegate = self;
//        _webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        _webView.scalesPageToFit = YES;
        _webView.multipleTouchEnabled = YES;
        _webView.userInteractionEnabled = YES;
        _webView.scrollView.scrollEnabled = YES;
        _webView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _webView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
